package fr.imag.adele.cprobes.impl;

import fr.imag.adele.cprobes.services.Scheduler;
import fr.imag.adele.cprobes.services.Probe;
import fr.imag.adele.cprobes.util.DataEvent;

/**
 * The standard implementation of a Scheduler Service.
 * It fires the probe each "interval" seconds, where "interval" 
 * is a configured property of the component.
 * 
 * @author Bassem DEBBABI
 *
 */
public class StandardScheduler implements Scheduler, Runnable {
	private long  	m_interval;			// the time interval between two ticks  
	private Probe 	m_probe;			// the controlled probe
	private boolean m_working;			// indicate whether the scheduler is working or not. 
	
	/**
	 * notify the scheduler for the arrival of an event
	 */
	public void notify(DataEvent dataEvent) {
		
	}
	/**
	 * fire the corresponding probe.
	 */
	public void fire() {
		if (getProbe() != null) {
			getProbe().process();
		}
	}
	/**
	 * called when the scheduler instance starts
	 */
	public void starting(){
		Thread t = new Thread(this);
		m_working=true;				
		t.start();		
	}
	/**
	 * called when the scheduler instance stops
	 */
	public void stoping(){
		m_working = false;
	}
	/**
	 * the run method of the thread
	 */
	public void run() {
		while (m_working==true){
			try {
				// fire
				fire();
				// wait t time			
				Thread.sleep(m_interval);
			} catch (InterruptedException e) {	
			}
		}
	}
	
	//// GETTERS and SETTERS	
	
	public long getInterval(){
		return m_interval;
	}
	public void setInterval(long interval){
		this.m_interval = interval;
	}
	public Probe getProbe() {
		return m_probe;
	}	
	public void setProbe(Probe probe) {
		m_probe = probe;
	}
	public void setWorking(boolean working) {
		this.m_working = working;		
	}
	public boolean isWorking() {
		return m_working;
	}
	////
}
